Add GtkCustomSorter
authorMatthias Clasen <mclasen@redhat.com>
Tue, 3 Dec 2019 04:43:14 +0000 (23:43 -0500)
committerMatthias Clasen <mclasen@redhat.com>
Sat, 30 May 2020 21:48:44 +0000 (17:48 -0400)
This is a GtkSorter implementation which uses a GCompareDataFunc.

docs/reference/gtk/gtk4-docs.xml
docs/reference/gtk/gtk4-sections.txt
docs/reference/gtk/gtk4.types.in
gtk/gtk.h
gtk/gtkcustomsorter.c [new file with mode: 0644]
gtk/gtkcustomsorter.h [new file with mode: 0644]
gtk/meson.build

index 18451cfae24912684a6ba82281b63d57467db094..dc6024ed4cb62a6cc4e6661be194a1e017306445 100644 (file)
@@ -62,6 +62,7 @@
       <xi:include href="xml/gtksortlistmodel.xml" />
       <section>
         <xi:include href="xml/gtksorter.xml" />
+        <xi:include href="xml/gtkcustomsorter.xml" />
       </section>
       <xi:include href="xml/gtkselectionmodel.xml" />
       <xi:include href="xml/gtknoselection.xml" />
index 614527aa0f721c868b38ae1f4c9553ed7f7489bd..d13c697ed63ea60953b2bf3baa12736d2d3329c6 100644 (file)
@@ -2441,6 +2441,21 @@ GTK_SORTER_GET_CLASS
 gtk_sorter_get_type
 </SECTION>
 
+<SECTION>
+<FILE>gtkcustomsorter</FILE>
+<TITLE>GtkCustomSorter</TITLE>
+GtkCustomSorter
+gtk_custom_sorter_new
+<SUBSECTION Standard>
+GTK_CUSTOM_SORTER
+GTK_IS_CUSTOM_SORTER
+GTK_TYPE_CUSTOM_SORTER
+GTK_IS_CUSTOM_SORTER_CLASS
+GTK_CUSTOM_SORTER_GET_CLASS
+<SUBSECTION Private>
+gtk_custom_sorter_get_type
+</SECTION>
+
 <SECTION>
 <FILE>gtksortlistmodel</FILE>
 <TITLE>GtkSortListModel</TITLE>
index 3b52d11d5123afb6459a259b48adea1b66836fb1..8ddec9caf57587fed93818a01eefa17655f19feb 100644 (file)
@@ -55,6 +55,7 @@ gtk_constraint_layout_get_type
 gtk_constraint_target_get_type
 gtk_css_provider_get_type
 gtk_custom_filter_get_type
+gtk_custom_sorter_get_type
 gtk_dialog_get_type
 gtk_directory_list_get_type
 gtk_drag_icon_get_type
index 4e0a62bf5b02d9cb2997d933b88c0cdfbdd1b7c5..bcd673a2e06cd3790b1661b8af764db9bdfdeabd 100644 (file)
--- a/gtk/gtk.h
+++ b/gtk/gtk.h
@@ -84,6 +84,7 @@
 #include <gtk/gtkconstraint.h>
 #include <gtk/gtkcssprovider.h>
 #include <gtk/gtkcustomlayout.h>
+#include <gtk/gtkcustomsorter.h>
 #include <gtk/gtkdebug.h>
 #include <gtk/gtkdialog.h>
 #include <gtk/gtkdirectorylist.h>
diff --git a/gtk/gtkcustomsorter.c b/gtk/gtkcustomsorter.c
new file mode 100644 (file)
index 0000000..6359fd6
--- /dev/null
@@ -0,0 +1,159 @@
+/*
+ * Copyright © 2019 Matthias Clasen
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Matthias Clasen <mclasen@redhat.com>
+ */
+
+#include "config.h"
+
+#include "gtkcustomsorter.h"
+
+#include "gtkintl.h"
+#include "gtktypebuiltins.h"
+
+/**
+ * SECTION:gtkcustomsorter
+ * @Title: GtkCustomSorter
+ * @Short_description: Sorting with a callback
+ *
+ * GtkCustomSorter is a #GtkSorter implementation that sorts
+ * via a traditional #GCompareDataFunc callback.
+ */
+struct _GtkCustomSorter
+{
+  GtkSorter parent_instance;
+
+  GCompareDataFunc sort_func;
+  gpointer user_data;
+  GDestroyNotify user_destroy;
+};
+
+G_DEFINE_TYPE (GtkCustomSorter, gtk_custom_sorter, GTK_TYPE_SORTER)
+
+static GtkOrdering
+gtk_custom_sorter_compare (GtkSorter *sorter,
+                           gpointer   item1,
+                           gpointer   item2)
+{
+  GtkCustomSorter *self = GTK_CUSTOM_SORTER (sorter);
+
+  if (!self->sort_func)
+    return GTK_ORDERING_EQUAL;
+
+  return gtk_ordering_from_cmpfunc (self->sort_func (item1, item2, self->user_data));
+}
+
+static GtkSorterOrder
+gtk_custom_sorter_get_order (GtkSorter *sorter)
+{
+  GtkCustomSorter *self = GTK_CUSTOM_SORTER (sorter);
+
+  if (!self->sort_func)
+    return GTK_SORTER_ORDER_NONE;
+
+  return GTK_SORTER_ORDER_PARTIAL;
+}
+
+static void
+gtk_custom_sorter_dispose (GObject *object)
+{
+  GtkCustomSorter *self = GTK_CUSTOM_SORTER (object);
+
+  if (self->user_destroy)
+    self->user_destroy (self->user_data);
+
+  self->sort_func = NULL;
+  self->user_destroy = NULL;
+  self->user_data = NULL;
+
+  G_OBJECT_CLASS (gtk_custom_sorter_parent_class)->dispose (object);
+}
+
+static void
+gtk_custom_sorter_class_init (GtkCustomSorterClass *class)
+{
+  GtkSorterClass *sorter_class = GTK_SORTER_CLASS (class);
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+
+  sorter_class->compare = gtk_custom_sorter_compare;
+  sorter_class->get_order = gtk_custom_sorter_get_order;
+
+  object_class->dispose = gtk_custom_sorter_dispose;
+}
+
+static void
+gtk_custom_sorter_init (GtkCustomSorter *self)
+{
+}
+
+/**
+ * gtk_custom_sorter_new:
+ * @sort_func: the #GCompareDataFunc to use for sorting
+ * @user_data: (nullable): user data to pass to @sort_func
+ * @user_destroy: (nullable): destroy notify for @user_data
+ *
+ * Creates a new #GtkSorter that works by calling
+ * @sort_func to compare items.
+ *
+ * Returns: a new #GTkSorter
+ */ 
+GtkSorter *
+gtk_custom_sorter_new (GCompareDataFunc sort_func,
+                       gpointer         user_data,
+                       GDestroyNotify   user_destroy)
+{
+  GtkCustomSorter *sorter;
+
+  sorter = g_object_new (GTK_TYPE_CUSTOM_SORTER, NULL);
+
+  gtk_custom_sorter_set_sort_func (sorter, sort_func, user_data, user_destroy);
+
+  return GTK_SORTER (sorter);
+}
+
+/**
+ * gtk_custom_sorter_set_sort_func:
+ * @self: a #GtkCustomSorter
+ * @sort_func: (nullable): function to sort items
+ * @user_data: (nullable): user data to pass to @match_func
+ * @user_destroy: destory notify
+ *
+ * Sets (or unsets) the function used for sorting items.
+ * 
+ * If the sort func changes its sorting behavior,
+ * gtk_sorter_changed() needs to be called.
+ *
+ * If a previous function was set, its @user_destroy will be
+ * called now.
+ **/
+void
+gtk_custom_sorter_set_sort_func (GtkCustomSorter  *self,
+                                 GCompareDataFunc  sort_func,
+                                 gpointer          user_data,
+                                 GDestroyNotify    user_destroy)
+{
+  g_return_if_fail (GTK_IS_CUSTOM_SORTER (self));
+  g_return_if_fail (sort_func || (user_data == NULL && !user_destroy));
+
+  if (self->user_destroy)
+    self->user_destroy (self->user_data);
+
+  self->sort_func = sort_func;
+  self->user_data = user_data;
+  self->user_destroy = user_destroy;
+
+  gtk_sorter_changed (GTK_SORTER (self), GTK_SORTER_CHANGE_DIFFERENT);
+}
diff --git a/gtk/gtkcustomsorter.h b/gtk/gtkcustomsorter.h
new file mode 100644 (file)
index 0000000..50cf090
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Copyright © 2019 Matthias Clasen
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Matthias Clasen <mclasen@redhat.com>
+ */
+
+#ifndef __GTK_CUSTOM_SORTER_H__
+#define __GTK_CUSTOM_SORTER_H__
+
+#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
+#error "Only <gtk/gtk.h> can be included directly."
+#endif
+
+#include <gtk/gtkexpression.h>
+#include <gtk/gtksorter.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_CUSTOM_SORTER             (gtk_custom_sorter_get_type ())
+GDK_AVAILABLE_IN_ALL
+G_DECLARE_FINAL_TYPE (GtkCustomSorter, gtk_custom_sorter, GTK, CUSTOM_SORTER, GtkSorter)
+
+GDK_AVAILABLE_IN_ALL
+GtkSorter *             gtk_custom_sorter_new                   (GCompareDataFunc        sort_func,
+                                                                 gpointer                user_data,
+                                                                 GDestroyNotify          user_destroy);
+
+GDK_AVAILABLE_IN_ALL
+void                    gtk_custom_sorter_set_sort_func         (GtkCustomSorter        *self,
+                                                                 GCompareDataFunc        sort_func,
+                                                                 gpointer                user_data,
+                                                                 GDestroyNotify          user_destroy);
+
+G_END_DECLS
+
+#endif /* __GTK_CUSTOM_SORTER_H__ */
index c8157ef2b56080c3cd642a9c8c7d7c3b3de1e44a..601024898636c3f10b9e6e0bd578fa432845176a 100644 (file)
@@ -201,6 +201,7 @@ gtk_public_sources = files([
   'gtkcssprovider.c',
   'gtkcustomfilter.c',
   'gtkcustomlayout.c',
+  'gtkcustomsorter.c',
   'gtkdialog.c',
   'gtkdirectorylist.c',
   'gtkdragicon.c',
@@ -461,6 +462,7 @@ gtk_public_headers = files([
   'gtkcssprovider.h',
   'gtkcustomfilter.h',
   'gtkcustomlayout.h',
+  'gtkcustomsorter.h',
   'gtkdebug.h',
   'gtkdialog.h',
   'gtkdirectorylist.h',